<div class="row animate fadeIn">
    <form class="form-horizontal" id="subInv_editform">
      <?php foreach($sub_data as $subs){?>
        <input type="hidden" name="hidden_sub_id_e" id="h_sub_id_e" value="<?php echo $subs->inv_sub_id; ?>"/>
    <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Edit</i></h5>
        <div class="ibox-tools">    
             <button class="btn btn-primary btn-sm save-btn-tab" id="submit-uom">Save</button>
             <a id="cancel_add" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
        </div>
     </div>
     <div class="ibox-content">
        
            <div class="form-group">
                <label class="col-lg-3 control-label">Sub-Inventory Code</label>
                <div class="col-lg-4">
                    <input type="text" class="form-control" id="" name="edit_sub_code" value ="<?php echo $subs->inv_sub_code; ?>">
                </div>
             </div>
             <div class="form-group">
                <label class="col-lg-3 control-label">Sub-Inventory Name</label>
                <div class="col-lg-4">
                    <input type="text" id=""class="form-control" name="edit_sub_name" value="<?php echo $subs->inv_sub_name; ?>">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Description</label>
                <div class="col-lg-4">
                    <input type="text" id="" class="form-control" name="edit_sub_desc" value="<?php echo $subs->description; ?>">
                </div>
            </div>
      </div>
      <?php } ?>
        </form>
        <div id="edit_locator_sched" class="ibox-content">
            <h3 style="color: #00afef">Create Locator</h3> 
            <form  id="sub_locator_eform" class="form-inline">
              <input type="hidden" name="hidden_sub_edit" id="sub_id_editForm">
            </br>
              <table id="SubTable"class="table table-bordered" style="width:80%;">
                <thead>
                    <tr>
                      <th style="">Locator Code</th>
                      <th style="">Description</th>
                      <th style="width:50px;">Action</th>
                    </tr>   
                    <tr>
                      <td style="margin:0px;padding:0px;">
                          <input style="width:100%;" type="text" id="" name="locate_code_e" class="form-control-custom" value="">
                      </td>
                      <td style="margin:0px;padding:0px;">
                          <input style="width:100%;" type="text" id="" name="locate_desc_e" class="form-control-custom">
                      </td>
                      <td style="margin:0px;padding:0px;">
                          <button style="width:100%;" class="btn btn-white" id=""><i class="fa fa-plus success"></i></button>
                      </td>
                    </tr>   
                </thead>
                </form>
                <tbody id="tbl_edit_locator" class="animate fadeIn">
                                                
                </tbody>
              </table> 
        </div>



<div class="modal fade" id="myModal" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
           <h4 class="modal-title" id="myModalLabel">Edit Sub-Locator</h4>
        </div>
      <div class="modal-body">
        <div class="row">
          <form id="editLocForm" method="post" class="form-horizontal">
              <input type="hidden" name="h_loc_id" value="">
              <div class="form-group">
                  <label class="col-lg-3 control-label">Locator Code</label>
                  <div class="col-lg-5">
                      <input type="text" name="locator_code_edit" class="form-control" value="" >
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Description</label>
                  <div class="col-lg-5">
                      <input type="text" name="locator_desc_edit" class="form-control" value="">
                  </div>
              </div>
          </form>
        </div>
      </div>
      <div class="modal-footer">
           <button id="update_inline_locator" type="button" class="btn btn-primary" >Save changes</button>
      </div>
    </div>
  </div>
</div> <!-- end-of-modal -->
    
 </div>
 <script>
    $(document).ready(function(){
    $('#subInv_editform').data('serialize',$('#subInv_editform').serialize());
    var hID = $('#h_sub_id_e').val();
    $('#sub_id_editForm').val(hID);
    generate_locators();

    function generate_locators(){
                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url($this->session->userdata('forajax') . '/get_locators_byID'); ?>",
                    data: { "sub_ids" : hID },
                    dataType: 'json',
                    success: function(data){
                        console.log(data);
                        var tmp = "";
                        var trHTML = "";
                         $.each(data, function(i, item) {
                            trHTML += '<tr><input type="hidden" value="'+data[i].locator_id+'" class="loc_id"><td text-align:right; class="code">' + data[i].locator_code + '</td><td text-align:right; class="desc">' + data[i].locator_description + '</td><td style="margin:0px;padding:0px;"> <a style="width:100%;" class="btn btn-outline btn-info" id="shw_edit_loc"><i class="fa fa-paste success"></i></a></td></tr>';
                        });
                        
                        $('#tbl_edit_locator').html(trHTML);
                    }
                });
    }

    $(document).on('click','#shw_edit_loc',function(){
          var loc_code = $(this).closest('tr').children('.code').text();
          var loc_desc = $(this).closest('tr').children('.desc').text();
          var locateid = $(this).closest('tr').children('.loc_id').val();
          $('#editLocForm input[name="locator_code_edit"]').val(loc_code);
          $('#editLocForm input[name="locator_desc_edit"]').val(loc_desc);
          $('#editLocForm input[name="h_loc_id"]').val(locateid);
          
          $('#myModal').modal('show');
        });


    $('#subInv_editform').validate({
        rules :{
            edit_sub_code:{
                required: true
            },
            edit_sub_name:{
                required: true
            }
        },submitHandler: function(form){
          dataString = $("#subInv_editform").serialize();
          $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax').'/update_sub_inv'); ?>",
            data: dataString,
            dataType: 'json',
          success: function(data){
            if(data[0].error == 1){
                notifymsg("danger",data[0].msg);
            }else{
                notifymsg("success",". . .New Sub-Inventory Added Successfully");
               
            }
          }      
         });   
        }
      });


      


      $('#sub_locator_eform').validate({
        rules :{
            locate_code_e:{
                required: true
            }
        },submitHandler: function(form){
          dataString = $("#sub_locator_eform").serialize();
          $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax').'/insert_sub_inv_locator_e'); ?>",
            data: dataString,
            dataType: 'json',
          success: function(data){
            if(data[0].error == 1){
                notifymsg("danger",data[0].msg);
            }else{
                notifymsg("success",". . .Sub-Inventory Locator Added Successfully");
                generate_locators();
                $('#sub_locator_eform')[0].reset();
            }   
          }   
         });   
        }
      });


    $('#editLocForm').validate({
      rules:{
        locator_code_edit: {
          required : true
        },
        locator_desc_edit: {
          required : true
        }
      }
    });


    $('#update_inline_locator').click(function(){
     if($("#editLocForm").valid()==true){
        dataString = $("#editLocForm").serialize();
          $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax').'/update_sub_inv_locator'); ?>",
            data: dataString,
            dataType: 'json',
          success: function(data){
            if(data[0].error == 1){
                notifymsg("danger",data[0].msg);
                 $('#myModal').modal('hide');
            }else{
                notifymsg("success",". . .Sub-Inventory Locator Added Successfully");
                generate_locators();
                 $('#myModal').modal('hide');
            }  
          }   
         }); 
     }
    });


      function alert_cancel(){
            if($('#subInv_editform').serialize()!=$('#subInv_editform').data('serialize')){
                // Form has changed!!!
                swal({
                                          title: "Are you sure?",
                                          text: "Your work will not be save",
                                          type: "warning",
                                          showCancelButton: true,
                                          confirmButtonColor: "#DD6B55",
                                          confirmButtonText: "Leave",
                                          cancelButtonText: "Stay",
                                          closeOnConfirm: true,
                                          closeOnCancel: true },
                                      function (isConfirm) {
                                          if (isConfirm) {
                                            swal("success");
                                              close_edit(); active_search();
                                              notifymsg("hide","hide");
                                          } else {
                                              swal("Cancelled");
                                          }
                                      });
                }else{
                close_edit(); active_search();
                notifymsg("hide","hide");
            }
        }
        $('#cancel_edit').click(function(){
            alert_cancel();
        });

       $('#editX').click(function(){
            alert_cancel();
       });
    });
 </script>  